package com.mxt.moviedemo.controller;


import com.mxt.moviedemo.entity.Reviews;
import com.mxt.moviedemo.entity.Ticket;
import com.mxt.moviedemo.service.ReviewsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("review")
public class ReviewController {
    private static final Logger logger = LoggerFactory.getLogger(ReviewController.class);
    @Autowired
    private ReviewsService reviewsService;
    @GetMapping("/searchReview")
    public Map<String, Object> searchReview(@RequestParam("uid") int uid) {
        logger.info("获取评论信息，id：{}", uid);
        Map<String, Object> result = new HashMap<>();

        try {
            List<Reviews> reviews=reviewsService.searchReviewsByUid(uid);
            if (reviews != null) {
                result.put("success", true);
                result.put("reviews", reviews);
            } else {
                result.put("success", false);
                result.put("error", "未找到该评论信息");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", "查询评论信息失败：" + e.getMessage());
        }
        return result;
    }
    @GetMapping("/infoReview")
    public Map<String, Object> infoReview(@RequestParam("rid") int rid) {
        logger.info("获取评论信息信息，id：{}", rid);
        Map<String, Object> result = new HashMap<>();

        try {
            Reviews reviews=reviewsService.getReviewsById(rid);
            if (reviews != null) {
                result.put("success", true);
                result.put("reviews", reviews);
            } else {
                result.put("success", false);
                result.put("error", "未找到该评论");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", "查询评论信息失败：" + e.getMessage());
        }
        return result;
    }

    // 新增订票
    @PostMapping("addReview")
    public Map<String, Object> addReview(@RequestBody Reviews reviews) {
        Map<String, Object> result = new HashMap<>();
        try {
            int rows = reviewsService.addReviews(reviews);
            if (rows > 0) {
                result.put("success", true);
                result.put("message", "评论添加成功");
            } else {
                result.put("success", false);
                result.put("message", "评论添加失败");
            }
        } catch (Exception e) {
            logger.error("添加评论错误: ", e);
            result.put("success", false);
            result.put("message", "评论添加失败：" + e.getMessage());
        }
        return result;
    }

    @PutMapping("updateReview")
    public Map<String, Object> updateReview(@RequestBody Reviews reviews) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (reviewsService.updateReviews(reviews)) {
                result.put("success", true);
                result.put("message", "评论信息修改成功");
            } else {
                result.put("success", false);
                result.put("message", "评论信息修改失败");
            }
        } catch (Exception e) {
            logger.error("评论信息错误: ", e);
            result.put("success", false);
            result.put("message", "评论信息修改失败：" + e.getMessage());
        }
        return result;
    }
    @DeleteMapping("deleteReview/{rid}")
    public Map<String, Object> deleteReview(@PathVariable int rid) {
        Map<String, Object> result = new HashMap<>();
        try {
            int rows = reviewsService.deleteReviews(rid);
            if (rows > 0) {
                result.put("success", true);
                result.put("message", "评论信息删除成功");
            } else {
                result.put("success", false);
                result.put("message", "评论信息删除失败");
            }
        } catch (Exception e) {
            logger.error("删除评论信息错误: ", e);
            result.put("success", false);
            result.put("message", "购票评论删除失败：" + e.getMessage());
        }
        return result;
    }
}
